import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from '@/router'
import config from '@/config'

const { base } = config
const whiteList = [`${base}/login`, `${base}/sso`]
export default {
  install() {
    NProgress.configure({ showSpinner: false })

    router.beforeEach(async (to, from) => {
      //  不在白名单
      if (!whiteList.includes(to.path)) {
        const token = localStorage.getItem('token')

        if (!token) {
          const loginPage = (import.meta.env.VITE_APP_BASE || '') + '/login'
          return loginPage
        }
      }
      NProgress.start()
    })

    router.afterEach(() => {
      NProgress.done()
    })
  },
}
